package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    // 处理员工查询分页请求 /employee/list
    @RequestMapping("/list")
    @RequiredPermission(name = "员工查询", expression = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        // 查询所有部门数据存到 Model 中
        model.addAttribute("departments", departmentService.listAll());

        return "employee/list"; // /WEB-INF/views/employee/list.jsp
    }

    // 处理删除请求 /employee/delete?id=1
    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list"; // localhost/employee/list
    }

    // 处理去保存或去修改请求 /employee/input[?id=1]
    @RequestMapping("/input")  //
    @RequiredPermission(name = "员工编辑", expression = "employee")
    public String input(Long id, Model model) {
        // 查询所有部门数据存到 Model 中
        model.addAttribute("departments", departmentService.listAll());
        // 查询所有角色存到 Model 中
        model.addAttribute("roles", roleService.listAll());

        // 去修改的话, id 有值
        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
            // 查询此员工对应角色数据,存到 Model 中
            model.addAttribute("ownRoles", roleService.queryByEmployeeId(id));
        }
        return "employee/input"; // /WEB-INF/views/employee/input.jsp
    }

    // 处理保存或者修改请求 /employee/saveOrUpdate 参数在请求体
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工编辑", expression = "employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] roleIds) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee, roleIds);
            } else {
                employeeService.update(employee, roleIds);
            }
            return new JsonResult(true, "编辑成功");
        } catch (Exception e) {
            return new JsonResult(false, "编辑失败");
        }

    }

    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String, Boolean> checkUsername(String username) {
        // 若存在 返回 true, 若不存在 false
        boolean exist = employeeService.checkUsername(username); // true
        Map<String, Boolean> data = new HashMap<>();
        data.put("valid", !exist);
        return data;
    }

    // 处理导出员工请求, 本质其实就是下载
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        // 获取响应对象输出流, 这样才可以把相应给用户下载文件写回浏览器
        workbook.write(response.getOutputStream());
    }

    // 处理导入员工请求, 本质其实就是上传
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        System.out.println(file.getName());
        System.out.println(file.getSize());
        // 根据用户上传的文件创建 Workbook 对象
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        employeeService.importXls(wb);
        return "redirect:/employee/list";
    }

}
